#include <QSettings>
#include <QFileDialog>
#include <QStringList>

#include "settings.h"


Settings::Settings(QWidget *parent)
    : QDialog(parent)
{
	ui.setupUi(this);
	load();
}

Settings::~Settings()
{

}

void Settings::on_okButton_clicked()
{
	save();
	accept();
}

void Settings::on_cancelButton_clicked()
{
	reject();
}

void Settings::on_asyPathButton_clicked()
{
	QString fn=QFileDialog::getOpenFileName(parentWidget(),tr("asy locale"),
			QDir::currentPath(),tr("asy program (*.*)"));
	if(fn.isEmpty())
		return;
	ui.asyPathEdit->setText(fn);
}

void Settings::load()
{
	QSettings set;
	set.beginGroup("Editor");
	QFont font=set.value("Font",qApp->font()).value<QFont>();
	ui.fontCombo->setCurrentFont(font);
	ui.fontSpin->setValue(font.pointSize());
    QString codec=set.value("Codec","UTF-8").toString();
    ui.codecCombo->setCurrentIndex(ui.codecCombo->findText(codec));
	set.endGroup();
	set.beginGroup("Tools");
	ui.asyPathEdit->setText(set.value("Asy Path","asy").toString());
	QStringList sl=set.value("Asy Parm",QStringList()).toStringList();
	ui.asyParmEdit->setText(sl.join(" "));
    ui.gsPathEdit->setText(set.value("GS Path","gs").toString());
    sl=set.value("GS Parm",QStringList()).toStringList();
    ui.gsParmEdit->setText(sl.join(" "));
	set.endGroup();

}

void Settings::save()
{
	QSettings set;
	set.beginGroup("Editor");
	QFont font=ui.fontCombo->currentFont();
	font.setPointSize(ui.fontSpin->value());
	set.setValue("Font",font);
    set.setValue("Codec",ui.codecCombo->currentText());
	set.endGroup();
	set.beginGroup("Tools");
	set.setValue("Asy Path",ui.asyPathEdit->text());
	QStringList sl=ui.asyParmEdit->text().split(" ",QString::SkipEmptyParts);
	set.setValue("Asy Parm",sl);
    set.setValue("GS Path",ui.gsPathEdit->text());
    sl=ui.gsParmEdit->text().split(" ",QString::SkipEmptyParts);
    set.setValue("GS Parm",sl);
	set.endGroup();

}

void Settings::on_gsPathButton_clicked()
{
	QString fn=QFileDialog::getOpenFileName(parentWidget(),tr("gs Locale"),
			QDir::currentPath(),tr("gs program (*.*)"));
	if(fn.isEmpty())
		return;
	ui.gsPathEdit->setText(fn);
}

